package com.loopy.servlet.response;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "DownServlet", value = "/down")
public class DownServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //sendRedirect本质会返回302状态码，location:/servlet/downNew会被解析成http://localhost:8080/ServletHomework/downNew

//        response.sendRedirect("/ServletHomework/downNew");
        String contextPath = getServletContext().getContextPath();
        response.sendRedirect(contextPath+"/downNew");
        //第二种方式
//        response.setStatus(302);
//        response.setHeader("Location","/ServletHomework/downNew");
    }
}
